<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;

/**
 * 示例接口
 */
class Forum extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['ForumList','getCourseCont','MynoteAndCollect','MakeNote','collect','Personal','CourseList'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

	public function ForumList(){
    	//$uid = Request::instance()->header()['uid'];
      	$uid = 2;
    	$Forum = Db::table('xmr_forum')->alias('f')->field('f.*,o.state,r.name,r.avatar')->join('xmr_order o', 'f.id=o.goods and o.uid='.$uid, 'left')->join('xmr_forum_role r','f.lecturer=r.id','left')->select();
      foreach($Forum as $k=>&$v){
          	$course = Db::table('xmr_course')->alias('c')->field('c.*,s.course_id,cc.content')->join('xmr_course_study s','c.id=s.course_id', 'left')->join('xmr_course_content cc','c.id=cc.course_id','left')->where('forum_id',$v['id'] )->group('c.periods')->order('c.id')->select();
          	$studyed = 0;$nowStudy = 0;
          	foreach($course as $kk=>$vv){
            	if(!empty($vv['course_id'])){
                	$studyed +=1;
                  	$nowStudy = $vv;
                }
            }
          	if(empty($v['state']) || $v['state'] == 1){
            	$v['state'] = 1;
              	$v['state_msg'] = '未报名';
            }else if($studyed < count($course)){
            	$v['state'] = 2;
              	$v['state_msg'] = '正在学习';
            }else if($studyed = count($course)){
            	$v['state'] = 3;
              	$v['state_msg'] = '完整课程';
            }
        	$v['nowStudy'] = $nowStudy;
          	$v['schedule'] = round($studyed/count($course)*100).'%';
          	$v['cover'] = 'http://'.$_SERVER['SERVER_NAME'].$v['cover'];
            $v['avatar'] = 'http://'.$_SERVER['SERVER_NAME'].$v['avatar'];
        }
      	return $Forum;
    }
  /**
   * 获取课节列表
   */
  public function CourseList($forumId){
      $course = Db::table('xmr_course')->alias('c')->field('c.*,s.course_id,f.course_syno,f.cover,r.name,f.title ftitle,f.price')->join('xmr_course_study s','c.id=s.course_id', 'left')->join('xmr_forum f','c.forum_id=f.id', 'left')->join('xmr_forum_role r','r.id = f.lecturer','left')->where('forum_id',$forumId)->order('c.id')->select();
       	
    		foreach($course as $k=>&$v){
            	if(!empty($v['course_id'])){
                  	$courseData['nowStudy'] = $v['id'];
                }
              	$courseData['cover'] = 'http://'.$_SERVER['SERVER_NAME'].$v['cover'];
              	$courseData['name'] = $v['name'];
              	$courseData['course_syno'] = $v['course_syno'];
              	$courseData['title'] = $v['ftitle'];
              	$courseData['price'] = $v['price'];
             	unset($v['course_syno']);unset($v['name']);unset($v['cover']);unset($v['ftitle']);unset($v['course_id']);unset($v['price']);
            }
        $courseData['course_num'] = count($course);
    	$courseData['course'] = $course; 
     return $courseData;
  }
  /**
   * 获取课程内容
   */
  	public function getCourseCont($courseId){
    	//$uid = Request::instance()->header()['uid'];
    	$uid = 2;	
      	$courseCont['courseCont'] = Db::table('xmr_course_content')->alias('c')->field('c.id,c.content,r.name,r.avatar,f.cover,r.islecturer,if(cc.collect is null,0,cc.collect) as collect,if(cc.note is null,0,cc.note) as note_state')->join('xmr_forum_role r','c.role_id=r.id','left')->join('xmr_course_contcollect cc','c.id=cc.course_contid and cc.uid='.$uid,'left')->join('xmr_course s','c.course_id=s.id','left')->join('xmr_forum f','f.id=s.forum_id','left')->where('c.course_id',$courseId)->order('c.id')->select();
		$courseCont['study'] = empty(Db::table('xmr_course_study')->where('course_id',$courseId)->where('uid',$uid)->find()) ? 0 : 1;
      	foreach($courseCont['courseCont'] as $k=>&$v){
            $v['avatar'] = 'http://'.$_SERVER['SERVER_NAME'].$v['avatar'];
          	$courseCont['cover'] = 'http://'.$_SERVER['SERVER_NAME'].$v['cover'];
            unset($v['cover']);
        }
      	$Relmark = Db::table('xmr_course_study')->where('uid', $uid)->where('course_id',$courseId)->find();
      	if(empty($Relmark)){
      		$marking = Db::table('xmr_course_study')->insert(['uid'=>$uid,'course_id'=>$courseId]);
        }
     	return $courseCont;
    }
  /**
   * 收藏
   */
  	public function collect($courseContId){
    	//$uid = Request::instance()->header()['uid'];
      	$uid = 2;
      	$res = Db::table('xmr_course_contcollect')->where('uid',$uid)->where('course_contid',$courseContId)->find();
      	if($res){
          	if($res['collect'] == 0){
            	$collect = Db::table('xmr_course_contcollect')->where('uid',$uid)->where('course_contid',$courseContId)->update(['collect'=>1,'collect_time'=>time()]);
              	if($collect){
                	return ['code'=>1, 'msg'=>'收藏成功'];
                }else{
                	return ['code'=>0, 'msg'=>'收藏失败'];
                }
            }else{
            	$nocollect = Db::table('xmr_course_contcollect')->where('uid',$uid)->where('course_contid',$courseContId)->update(['collect'=>0]);
              	if($nocollect){
                	return ['code'=>1, 'msg'=>'取消收藏成功'];
                }else{
                	return ['code'=>0, 'msg'=>'取消收藏失败'];
                }
            }
        }else{
            $collectData = ['course_contid'=>$courseContId,'uid'=>$uid,'collect'=>1,'collect_time'=>time()];
    	 	$collect = Db::table('xmr_course_contcollect')->insert($collectData);
              	if($collect){
                	return ['code'=>1, 'msg'=>'收藏成功'];
                }else{
                	return ['code'=>0, 'msg'=>'收藏失败'];
                }
        }
    }
  /**
   * 做笔记
   */
  	public function MakeNote($courseContId, $note){
    	$uid = Request::instance()->header()['uid'];
      	$res = Db::table('xmr_course_contcollect')->where('uid',$uid)->where('course_contid',$courseContId)->find();
      	if($res){
            $makenote = Db::table('xmr_course_contcollect')->where('uid',$uid)->where('course_contid',$courseContId)->update(['note'=>$note,'makenote_time'=>time()]);
              	if($makenote){
                	return ['code'=>1, 'msg'=>'做笔记成功'];
                }else{
                	return ['code'=>0, 'msg'=>'做笔记失败'];
                }
        }else{
            $makenoteData = ['course_contid'=>$courseContId,'uid'=>$uid,'note'=>$note,'makenote_time'=>time()];
    	 	$makenote = Db::table('xmr_course_contcollect')->insert($makenoteData);
              	if($makenote){
                	return ['code'=>1, 'msg'=>'做笔记成功'];
                }else{
                	return ['code'=>0, 'msg'=>'做笔记失败'];
                }
        }
    }
  /**
   * 我的日记和收藏
   */
  	public function MynoteAndCollect(){
    	$uid = Request::instance()->header()['uid'];
    	$myCollect = Db::table('xmr_course_contcollect')->alias('cc')->field('cc.collect_time,c.content,f.title')->join('xmr_course_content c','c.id=cc.course_contid')->join('xmr_course s','s.id=c.course_id')->join('xmr_forum f','f.id=s.forum_id')->where('cc.uid',$uid)->where('cc.collect',1)->select();
    	$myNote = Db::table('xmr_course_contcollect')->alias('cc')->field('cc.note,cc.makenote_time,c.content,f.title')->join('xmr_course_content c','c.id=cc.course_contid')->join('xmr_course s','s.id=c.course_id')->join('xmr_forum f','f.id=s.forum_id')->where('cc.uid',$uid)->where('cc.note is not null')->select();
    	return ['myCollect'=>$myCollect,'myNote'=>$myNote];
    }
  /**
   * 个人中心
   */
  	public function Personal(){
    	$uid = Request::instance()->header()['uid'];
      	$userInfo = Db::table('xmr_user')->alias('u')->field('u.nickname,u.avatar,u.student_id,u.credit,count(o.order_no)as forum_num')->join('xmr_order o','u.id=o.uid','left')->where('u.id',$uid)->find();
      	return $userInfo;
    }
}
